#include "imageGraphics.h"
#include "commonDefine.h"
#include "securec.h"

#include "sample_comm.h"

#define RGN_CROSSHAIR_WIDTH  166
#define RGN_CROSSHAIR_HEIGHT 166
#define RGN_ALPHA 128
#define RGN_ARGB8888_BLUE 0xff0000ff
#define RGN_RGB888_BLUE 0x0000ff
#define RGN_RGB888_RED  0xff0000

#define OT_RGN_CLUT_NUM                16

#define CROSSHAIR_CANVAS_NUM 1


int vpss_chn_add_osd(ot_vpss_grp grpID,ot_vpss_chn chnID,const char* osdPath){
    
    return TD_SUCCESS;
}



int vgsDrawRectangle(ot_video_frame_info *image_in, image_detect_info &detectInfo)
{
    

    ot_vgs_handle handle_vgs = {};
    td_s32 s32Ret = ss_mpi_vgs_begin_job(&handle_vgs);
    if(s32Ret != TD_SUCCESS){
        printf("error ss_mpi_vgs_begin_job with %x\n",s32Ret);
        return s32Ret;
    }

    ot_vgs_task_attr task_attr = {};
    if (memcpy_s(&task_attr.img_in, sizeof(ot_video_frame_info),
        image_in, sizeof(ot_video_frame_info)) != EOK) {
        ERRO_PRT("memcpy_s img_in failed\n");
        return TD_FAILURE;
    }

    if (memcpy_s(&task_attr.img_out, sizeof(ot_video_frame_info),
        image_in, sizeof(ot_video_frame_info)) != EOK) {
        ERRO_PRT("memcpy_s img_out failed\n");
        return TD_FAILURE;
    }

    // 临时添加十字靶标绘制线，后期改为 rgn overlay
    ot_vgs_line crosshair[4];
    // top
    crosshair[0].start_point = {VI_INPUT_WIDTH/4,VI_INPUT_HEIGHT/4 - 60};
    crosshair[0].end_point = {VI_INPUT_WIDTH/4,VI_INPUT_HEIGHT/4 - 10};
    crosshair[0].thick = 4;
    crosshair[0].color = 0x00FF00;

    // bottom
    crosshair[1].start_point = {VI_INPUT_WIDTH/4,VI_INPUT_HEIGHT/4 + 60};
    crosshair[1].end_point = {VI_INPUT_WIDTH/4,VI_INPUT_HEIGHT/4 + 10};
    crosshair[1].thick = 4;
    crosshair[1].color = 0x00FF00;

    // left
    crosshair[2].start_point = {VI_INPUT_WIDTH/4 -60,VI_INPUT_HEIGHT/4 };
    crosshair[2].end_point = {VI_INPUT_WIDTH/4 - 10,VI_INPUT_HEIGHT/4 };
    crosshair[2].thick = 4;
    crosshair[2].color = 0x00FF00;

    // right
    crosshair[3].start_point = {VI_INPUT_WIDTH/4 + 60,VI_INPUT_HEIGHT/4};
    crosshair[3].end_point = {VI_INPUT_WIDTH/4 + 10,VI_INPUT_HEIGHT/4};
    crosshair[3].thick = 4;
    crosshair[3].color = 0x00FF00;


    s32Ret = ss_mpi_vgs_add_draw_line_task(handle_vgs,&task_attr,crosshair,4);
    if(s32Ret != TD_SUCCESS){
        ERRO_PRT("error ss_mpi_vgs_add_draw_line_task %x\n",s32Ret);

    }


    ot_corner_rect *corner_rect = nullptr;
    ot_corner_rect_attr *corner_rect_attr = nullptr;
    if (detectInfo.detect.empty() == false)
    {

        size_t rect_nums = detectInfo.detect.size();
        corner_rect = new ot_corner_rect[rect_nums];
        corner_rect_attr = new ot_corner_rect_attr[rect_nums];

        for (size_t i(0); i < rect_nums && i < MAX_RECT_DRAW; i++)
        {

            // 矩形参数设置
            corner_rect[i].thick = 2;

            corner_rect[i].rect.x = detectInfo.detect[i].x / 2 + ((detectInfo.detect[i].x / 2) & 1);
            corner_rect[i].rect.y = detectInfo.detect[i].y / 2 + ((detectInfo.detect[i].y / 2) & 1);
            corner_rect[i].rect.width = detectInfo.detect[i].w / 2 + ((detectInfo.detect[i].w / 2) & 1);
            corner_rect[i].rect.height = detectInfo.detect[i].h / 2 + ((detectInfo.detect[i].h / 2) & 1);

            corner_rect[i].hor_len = corner_rect[i].rect.width * 2;
            corner_rect[i].ver_len = corner_rect[i].rect.height * 2;

            // INFO_PRT("rect info x %d,y %d,w %d,h %d\n",
            //             corner_rect[i].rect.x,corner_rect[i].rect.y,corner_rect[i].rect.width,corner_rect[i].rect.height);
            // 属性设置
            corner_rect_attr[i].color = 0xFF0000;
            corner_rect_attr[i].corner_rect_type = OT_CORNER_RECT_TYPE_FULL_LINE;
        }

        s32Ret = ss_mpi_vgs_add_corner_rect_task(handle_vgs, &task_attr, corner_rect, corner_rect_attr, rect_nums);
        if (s32Ret != TD_SUCCESS)
        {
            //printf("error ss_mpi_vgs_add_corner_rect_task with %x\n", s32Ret);
            // goto finsh;
        }
    }

    s32Ret = ss_mpi_vgs_end_job(handle_vgs);
    if(s32Ret != TD_SUCCESS){
        printf("error ss_mpi_vgs_end_job with %x\n",s32Ret);
        ss_mpi_vgs_cancel_job(handle_vgs);
        goto finsh;
    }


finsh:
    if (corner_rect)
    {
        delete[] corner_rect;
        delete[] corner_rect_attr;
    }

    return s32Ret;
}
